package rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 创建连接⼯⼚
        ConnectionFactory factory = new ConnectionFactory();
        //2. 设置参数
        factory.setHost("192.168.247.132");//ip 默认值localhost
        factory.setPort(5672); //默认值5672
        factory.setVirtualHost("rhm");//虚拟机名称, 默认 /
        factory.setUsername("admin");//⽤⼾名,默认guest
        factory.setPassword("admin");//密码, 默认guest
        //3. 创建连接Connection
        Connection connection = factory.newConnection();
        //4. 创建channel通道
        Channel channel = connection.createChannel();
        //5.声明队列
        channel.queueDeclare("hello",true,false,false,null);
        //6.发送消息
        String msg = "Hello rabbitmq";
        channel.basicPublish("","hello",null,msg.getBytes());
        //7.资源释放
        channel.close();
        connection.close();
    }
}
